#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <getopt.h>
#include <netdb.h>
//#define SERVER_IP     "192.168.0.8"
//#define SERVER_PORT   12345
#define MSG_STR         "Hello ,KAMEN RIDER!"

void print_usage(char *progname)
{
        printf("%s usage: \n",progname);
        printf("-i(--ipaddr): specify server IP address.\n");
        //printf("-d(--servertname): specify host name.\n");
        printf("-p(--port): specify server port.\n");
        printf("-h(--Help): print this help information.\n");

        return;
}



int main (int argc,char *argv[])
{
        int                     socket_fd;
        int                     rv = -1;
        struct sockaddr_in      server_addr;
        char                    buf[1024];
        char                    *server_ip;
       // char                    *server_name = NULL;
        int                     port;
        int                     ch = 0;
        unsigned long           add = -1;
        struct hostent          *h;
        struct in_addr          in;
        struct option           opts[] = {
                {"ipaddr",required_argument,NULL,'i'},
                //{"host",optional_argument,NULL,'d'},
                {"port",required_argument,NULL,'p'},
                {"help",no_argument,NULL,'h'},
                {NULL,0,NULL,0}
        };


        while((ch = getopt_long(argc,argv,"i:p:h",opts,NULL)) != -1)
        {
                switch(ch)
                {
                        case 'i':
                                server_ip = optarg;
                                break;
                       /* case 'd':
                                server_name = optarg;
                                break;
				*/
                        case 'p':
                                port = atoi(optarg);
                                break;
                        case 'h':
                                print_usage(argv[0]);
                                return 0;
                }
        }

        if(!port || !server_ip) 
        {
                print_usage(argv[0]);
                return 0;
        }
        //printf("%s usage: \n",prog:ame);


        socket_fd = socket(AF_INET,SOCK_STREAM,0);
        if(socket_fd < 0)
        {
                printf("Create socket failure: %s\n",strerror(errno));
                return -1;
        }
        printf("Create socket[%d] successful!\n",socket_fd);
       


        memset(&server_addr,0,sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(port);


        add = inet_addr(server_ip);
        if(add == INADDR_NONE)
        {
                printf("不是有效的ip地址，可能是域名\n");
                h = gethostbyname(server_ip);
                if(h==NULL)
         {
                printf("%s\n",hstrerror(h_errno));
         }
        else
         {
                memcpy(&server_addr.sin_addr.s_addr,h->h_addr,4);
                in.s_addr=server_addr.sin_addr.s_addr;
                //printf("host name:%s\n",h->h_name);
                //printf("ip lenght:%d\n",h->h_length);//IPv4 or IPv6
                //printf("type:%d\n",h->h_addrtype);
                printf("ipaddr:%s\n",inet_ntoa(in));//将一个IP转换成一个互联网标准点分格式的字符串
                server_ip = inet_ntoa(in); 

         }



        }
	
	inet_aton(server_ip,&server_addr.sin_addr);
	

        rv = connect(socket_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
        if( rv < 0)
        {
                printf("Connect to server[%s:%d] failure: %s\n",server_ip,port,strerror(errno));
                return -2;
        }
        printf("Connect to server[%s:%d] successful!\n",server_ip,port);
        while(1)
   {
        //printf("Start accept new client incoming...\n");
        rv = write(socket_fd,MSG_STR,strlen(MSG_STR));
        if( rv < 0 )
        {
                printf("Write to server by socket_fd[%d] failure: %s\n",socket_fd,strerror(errno));
                return -3;
        }
        printf("Write to server by socket_fd[%d] successful!\n",socket_fd);
   

        memset(buf,0,sizeof(buf));
        rv = read(socket_fd,buf,sizeof(buf));
        if( rv < 0 )
        {
                printf("Read data from server by socket_fd[%d] failure: %s\n ",socket_fd,strerror(errno));
                return -4;
        }
        else if( rv == 0)
        {
                printf("socket_fd[%d] get disconnected\n",socket_fd);
                return -5;
        }
        printf("Read %d bytes data from server :%s\n",rv,buf);
   }
        close(socket_fd);
   
}
